package com.red.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.red.entity.Product;
import com.red.result.Result;
import com.red.service.ProductService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @ApiOperation("根据条件查询商品")
    @ApiImplicitParam(name = "name",value = "商品名称")
    @GetMapping("/select")
    public Result findAll(String name){
        return new Result(
                productService.selectProductList(
                        new QueryWrapper<Product>()
                                .like(name!=null,"name",name)
                )
        );
    }

    @ApiOperation("添加商品")
    @PostMapping("/save")
    public Result save(Product product){
        return new Result(productService.save(product));
    }

    @ApiOperation("修改商品")
    @PutMapping("/update")
    public Result update(Product product){
        return new Result(productService.updateById(product));
    }

    @ApiOperation("删除商品")
    @DeleteMapping("/delete")
    public Result delete(Integer id){
        return  new Result(productService.removeById(id));
    }
}
